package com.ellricky.baselib.adapter;

import android.app.Activity;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.cardview.widget.CardView;

import com.andr.common.ui.adapter.BaseRecyclerAdapter;
import com.andr.common.ui.adapter.SmartViewHolder;
import com.andr.common.util.EmptyUtil;
import com.andr.common.util.ResUtil;
import com.andr.common.util.StrUtil;
import com.ellricky.baselib.R;
import com.ellricky.baselib.entity.PersonItemEntity;
import com.ellricky.baselib.util.GlideUtil;

import de.hdodenhof.circleimageview.CircleImageView;

public class PersonGridAdapter extends BaseRecyclerAdapter<PersonItemEntity> {

    private Activity activity;

    private boolean showSex;

    public PersonGridAdapter(Activity activity) {
        super(R.layout.base_layout_adapter_attend);
        this.activity = activity;
    }

    public void setShowSex(boolean showSex) {
        this.showSex = showSex;
    }

    //    public PersonGridAdapter(Activity activity) {
//        super(R.layout.base_layout_adapter_attend);
//        this.activity = activity;
//    }


    @Override
    public void onBindViewHolder(SmartViewHolder holder, PersonItemEntity model, int position) {
        CardView cvRoot = holder.itemView.findViewById(R.id.cvRoot);

        ImageView addView = holder.itemView.findViewById(R.id.ivAdd);
        View personView = holder.itemView.findViewById(R.id.personView);


        boolean canAdd = model == null;
        if (canAdd) {
            addView.setVisibility(View.VISIBLE);
            personView.setVisibility(View.GONE);
            cvRoot.setCardBackgroundColor(ResUtil.getColor(activity, R.color.common_gray_ef));
        } else {
            addView.setVisibility(View.GONE);
            personView.setVisibility(View.VISIBLE);
            cvRoot.setCardBackgroundColor(ResUtil.getColor(activity, R.color.common_main_white));

            CircleImageView iv = holder.itemView.findViewById(R.id.ivPic);
            TextView tvName = holder.itemView.findViewById(R.id.tvName);
            GlideUtil.into(activity, model.getHeadPic(), iv);

            if (showSex) {
                String name = StrUtil.filterEmpty(model.getName());
                String sexName = StrUtil.filterEmpty(model.getSexName());
                if (!EmptyUtil.isEmpty(sexName)) name += "(" + sexName +")";
                tvName.setText(name);
                tvName.setTextSize(TypedValue.COMPLEX_UNIT_DIP,12);
            } else {
                tvName.setText(model.getName());
            }

        }
    }
}
